import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import {ElMessage} from 'element-plus'
import {useMenuStore} from '@/store/menu'
import {useUserStore} from '@/store/user'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/index.vue'),
        meta: {title: '登录', hidden: true}
    },
    {
        path: '/',
        component: () => import('@/layout/index.vue'),
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: () => import('@/views/dashboard/index.vue'),
                meta: {title: '首页', icon: 'House'}
            }
        ]
    },
    {
        path: '/profile',
        name: 'Profile',
        component: () => import('@/views/profile/index.vue'),
        meta: {title: '个人信息', hidden: true}
    },
    {
        path: '/system',
        component: () => import('@/layout/index.vue'),
        redirect: '/system/user',
        meta: {title: '系统管理', icon: 'Setting'},
        children: [
            {
                path: 'user',
                name: 'User',
                component: () => import('@/views/system/user/index.vue'),
                meta: {title: '用户管理', icon: 'User'}
            },
            {
                path: 'role',
                name: 'Role',
                component: () => import('@/views/system/role/index.vue'),
                meta: {title: '角色管理', icon: 'UserFilled'}
            },
            {
                path: 'menu',
                name: 'Menu',
                component: () => import('@/views/system/menu/index.vue'),
                meta: {title: '菜单管理', icon: 'Menu'}
            },
            {
                path: 'dept',
                name: 'Dept',
                component: () => import('@/views/system/dept/index.vue'),
                meta: {title: '部门管理', icon: 'OfficeBuilding'}
            },
            {
                path: 'log',
                name: 'OperationLog',
                component: () => import('@/views/system/logView/index.vue'),
                meta: {title: '操作日志', icon: 'Document'}
            }
        ]
    },
    {
        path: '/server',
        component: () => import('@/layout/index.vue'),
        redirect: '/server/list',
        meta: {title: '服务器管理', icon: 'Monitor'},
        children: [
            {
                path: 'list',
                name: 'ServerList',
                component: () => import('@/views/server/list/index.vue'),
                meta: {title: '服务器列表', icon: 'List'}
            },
            {
                path: 'monitor',
                name: 'ServerMonitor',
                component: () => import('@/views/server/monitor/index.vue'),
                meta: {title: '服务器监控', icon: 'DataLine'}
            }
        ]
    },
    {
        path: '/content',
        component: () => import('@/layout/index.vue'),
        redirect: '/content/list',
        meta: {title: '文章管理', icon: 'Folder'},
        children: [
            {
                path: 'list',
                name: 'ContentList',
                component: () => import('@/views/content/list/index.vue'),
                meta: {title: '文章列表', icon: 'Document'}
            }
        ]
    },
    {
        path: '/tools',
        component: () => import('@/layout/index.vue'),
        redirect: '/tools/list',
        meta: {title: '脚本工具', icon: 'Folder'},
        children: [
            {
                path: 'list',
                name: 'ToolsList',
                component: () => import('@/views/tools/list/index.vue'),
                meta: {title: '代码生成', icon: 'Document'}
            }
        ]
    },
    {
        path: '/redirect/:path(.*)*',
        name: 'Redirect',
        component: () => import('@/views/redirect/index.vue'),
        meta: { hidden: true }
    }
]


const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
    document.title = to.meta.title ? `${to.meta.title} - 后台管理平台` : '后台管理平台'
    const token = localStorage.getItem('token')
    if (to.path === '/login') {
        if (token) {
            next('/')
        } else {
            next()
        }
        return
    }
    if (!token) {
        ElMessage.warning('请先登录')
        next({
            path: '/login',
            query: {redirect: to.fullPath}
        })
        return
    }
    // 只在首次进入且未加载菜单时拉取菜单
    const menuStore = useMenuStore()
    const userStore = useUserStore()
    if (!menuStore.menuLoaded) {
        await userStore.fetchMenuList()
        next()
        return
    }
    next()
})

export default router
